﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using XQ.SP.Model;
using XQ.SP.Common;

namespace System
{
    public class UsersInfo
    {
        #region
        /// <summary>
        /// 获取后台
        /// </summary>
        /// <returns></returns>
        public static XQ.SP.Model.UserInfoItem GetUserH()
        {
            if (HttpContext.Current.Session["Users"] != null)
            {
                XQ.SP.Model.UserInfoItem item = (XQ.SP.Model.UserInfoItem)HttpContext.Current.Session["Users"];
                return item;
            }
            string uid = GetUsersID();
            if (string.IsNullOrEmpty(uid))
            {
                XQ.SP.BllServices.UserInfoBLL bllui = new XQ.SP.BllServices.UserInfoBLL();
                var item = bllui.GetModel(1);
                return item;
            }
            else
            {
                XQ.SP.BllServices.UserInfoBLL bllui = new XQ.SP.BllServices.UserInfoBLL();
                var item = bllui.GetModel(Int64.Parse(uid));
                return item;
            }
        }
        #endregion

        #region 前端用户

        public static XQ.SP.Model.UsersItem GetUser()
        {
            if (HttpContext.Current.Session["Users"] != null)
            {
                XQ.SP.Model.UsersItem item = (XQ.SP.Model.UsersItem)HttpContext.Current.Session["Users"];
                return item;
            }
            string uid = GetUsersID();
            if (string.IsNullOrEmpty(uid))
            {
                return null;
            }
            else
            {
                XQ.SP.BllServices.UsersBLL bllui = new XQ.SP.BllServices.UsersBLL();
                var item = bllui.GetModel(Int64.Parse(uid));
                return item;
            }
        }

        public static void LoginUser(XQ.SP.Model.UsersItem modelU)
        {
            HttpContext.Current.Session["Users"] = modelU;
            //确认用户登录 写入cookies
            XQ.Common.Cookies.CreateTicket(true, modelU.UserName + "[,]" + modelU.Id + "[,]" + 0 + "[,]学校[,]", "member");
        }

        /// <summary>
        /// 获取当前用户ID
        /// </summary>
        /// <returns></returns>
        public static string GetUsersID()
        {
            return "1";
            try
            {
                string tempStr = HttpContext.Current.User.Identity.Name.Substring(HttpContext.Current.User.Identity.Name.IndexOf("[,]") + 3);
                tempStr = tempStr.Substring(0, tempStr.IndexOf("[,]"));

                if (string.IsNullOrEmpty(tempStr))
                {
                    XQ.SP.Model.UsersItem item = (XQ.SP.Model.UsersItem)HttpContext.Current.Session["Users"];
                    if (item == null)
                    {
                        return null;
                    }
                    else
                    {
                        return item.Id.ToString();
                    }
                }
                else
                {
                    return tempStr;
                }
            }
            catch
            {
                XQ.SP.Model.UsersItem item = (XQ.SP.Model.UsersItem)HttpContext.Current.Session["Users"];
                if (item == null)
                {
                    return null;
                }
                else
                {
                    return item.Id.ToString();
                }
            }
        }

        /// <summary>
        /// 获取当前用户名称
        /// </summary>
        /// <returns></returns>
        public static string GetUsersName()
        {
            try
            {
                string tempStr = HttpContext.Current.User.Identity.Name.Substring(0, HttpContext.Current.User.Identity.Name.IndexOf("[,]"));

                if (string.IsNullOrEmpty(tempStr))
                {
                    XQ.SP.Model.UsersItem item = (XQ.SP.Model.UsersItem)HttpContext.Current.Session["Users"];
                    if (item == null)
                    {
                        return tempStr;
                    }
                    else
                    {
                        return item.UserName;
                    }
                }

                return tempStr;
            }
            catch
            {
                XQ.SP.Model.UsersItem item = (XQ.SP.Model.UsersItem)HttpContext.Current.Session["Users"];
                if (item == null)
                {
                    return "";
                }
                else
                {
                    return item.UserName;
                }
            }
        }
        #endregion

    }

    /// <summary>
    /// MVC控件编写成代码
    /// </summary>
    public static class HtmlHelperMy
    {
        //Ajax.ActionLink 添加个图片链接
        public static IHtmlString WriteHtml(string str)
        {
            return new HtmlString(HttpUtility.HtmlDecode(str.ToString()));
        }
    }
}
